/**
 *====================================================
* 文件名称: EnterpriseGradeEvaluationServiceImpl.java
 * 修订记录：
  * No    日期				作者(操作:具体内容)
  * 1.    2015年12月14日	chenxy(创建:创建文件)
  *====================================================
  * 类描述：(说明未实现或其它不应生成javadoc的内容)
  */
package com.yuexunit.finance.member.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.member.entity.EnterpriseEntity;
import com.yuexunit.finance.member.entity.EnterpriseGradeEvaluationEntity;
import com.yuexunit.finance.member.entity.lexicon.AuditStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;

/**
 * @ClassName: EnterpriseGradeEvaluationServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年12月14日 22:01:41
 */
@Implements(EnterpriseGradeEvaluationService.class)
public class EnterpriseGradeEvaluationServiceImpl implements EnterpriseGradeEvaluationService {

	@Override
	@Transactional
	public Long createEnterpriseGradeEvaluation(EnterpriseGradeEvaluationEntity entity) {
		PersistUtil.insert(entity);
		return entity.getGradeEvaluationId();
	}
	
	@Override
	@Transactional
	public void updateEnterpriseGradeEvaluation(EnterpriseGradeEvaluationEntity entity) {
		PersistUtil.update(entity);
	}

	@Override
	public PageList<EnterpriseGradeEvaluationEntity> inquireEnterpriseGradeEvaluationPageList(Long enterpriseId, String enterpriseName, Integer auditState, PageCriteria page) {
		List<Long> enterpriseIds = null;
		if (StringUtils.isNotBlank(enterpriseName)) {
			DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseEntity.class);
			if (enterpriseName !=null) {
				criteria.add(Restrictions.like("enterpriseName",  "%" + enterpriseName + "%" ));
			}
			criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("enterpriseId")));
			enterpriseIds = PersistUtil.get(criteria);
			if (enterpriseIds.isEmpty()) {
				return new PageList<>(page);
			}
		}
		if (enterpriseId != null) {
			if (enterpriseIds == null) {
				enterpriseIds = new ArrayList<>();
			}
			enterpriseIds.add(enterpriseId);
		}
		DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseGradeEvaluationEntity.class);
		if(enterpriseIds !=null){
			criteria.add(Restrictions.in("enterpriseId", enterpriseIds));
		}
		if (auditState !=null && auditState > 0) {
			criteria.add(Restrictions.eq("auditState", AuditStateEnum.valueOf(auditState)));
		}
		criteria.add(Restrictions.isNull("isCreate"));
		criteria.addOrder(Order.desc("evaluationDateTime"));
		return PersistUtil.get(criteria, page);
	}
	
	@Override
	public List<EnterpriseGradeEvaluationEntity> inquireEnterpriseGradeEvaluationList(Long enterpriseId,
			Integer auditState) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseGradeEvaluationEntity.class);
		criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		criteria.add(Restrictions.eq("auditState", AuditStateEnum.valueOf(auditState)));
		return PersistUtil.get(criteria);
	}

	@Override
	public EnterpriseGradeEvaluationEntity inquireEnterpriseGradeEvaluation(Long enterpriseId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseGradeEvaluationEntity.class);
		criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		List<EnterpriseGradeEvaluationEntity> list = PersistUtil.get(criteria);
		System.out.println(list.size() - 1);
		Integer last = list.size() - 1;
		if (list.isEmpty()) {
			return null;
		} else {
			return list.get(last);
		}
	}

	@Override
	public EnterpriseGradeEvaluationEntity inquireEnterpriseGradeEvaluationDetail(Long gradeEvaluationId) {
		if (gradeEvaluationId == null) {
			return null;
		}
		return PersistUtil.get(EnterpriseGradeEvaluationEntity.class, gradeEvaluationId);
	}

	@Override
	@Transactional
	public void auditEnterpriseGradeEvaluation(EnterpriseGradeEvaluationEntity e) {
		EnterpriseGradeEvaluationEntity entity = PersistUtil.get(EnterpriseGradeEvaluationEntity.class, e.getGradeEvaluationId());
		entity.setAuditDate(e.getAuditDate());
		entity.setAuditor(e.getAuditor());
		entity.setAuditRemark(e.getAuditRemark());
		entity.setAuditState(e.getAuditState());
		PersistUtil.update(entity);
	}

	@Override
	public List<EnterpriseGradeEvaluationEntity> inquireEnterpriseGradeEvaluationPageByStateList(Integer[] state) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseGradeEvaluationEntity.class);
		if (state != null && state.length > 0) {
			List<AuditStateEnum> financeStateList = new ArrayList<>();
			for (Integer f : state) {
				if (f == null) {
					continue;
				}
				financeStateList.add(AuditStateEnum.valueOf(f));
			}
			if (financeStateList.size() > 0) {
				criteria.add(Restrictions.in("auditState", financeStateList.toArray(new AuditStateEnum[financeStateList.size()])));
			}
		}
		criteria.addOrder(Order.desc("evaluationDateTime"));
		return PersistUtil.get(criteria);
	}
}
